﻿//============================================================
// Project: DigitalFactory
// Author: Administrator@DESKTOP-IE3FPT7
// Datetime: 2021-03-24 11:28:27
// Description: TODO >> This is a script Description.
//============================================================

using System.Collections;
using UnityEngine;

namespace NodePathFinder.Script
{
    public class PathFinderAgent
    {
        private GameObject _GameObject;
        private NodeTypeMapEntity _NodeTypeMapEntity;
        private Transform _Transform;
        public float Speed { get; set; } = .1f;

        /// <summary>
        /// 当前所在的边。
        /// a ------- O ------b:如果在中间则返回该边。
        /// b-------------- Oa --------------c:如果在点a上，则会返回ba,ac两条边
        /// </summary>
        public Edge[] StayEdge;
        private int _LayerMask;

        private int _StayNode = -1;
        private int _DestNode = -1;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameObject"></param>
        /// <param name="nodeTypeMapEntity"></param>
        public PathFinderAgent(GameObject gameObject, NodeTypeMapEntity nodeTypeMap) 
        {
            this._GameObject = gameObject;
            this._Transform = _GameObject.transform;
            this._NodeTypeMapEntity = nodeTypeMap;
            this._LayerMask = LayerMask.GetMask("Environment", "Result Object");
        }

        public void Force2Node(int index) 
        {
            _Transform.position = _NodeTypeMapEntity.Nodes[index];
            _StayNode = index;
        }

        public void SetDestPoint(int index) 
        {
            _DestNode = index;
          
            _NodeTypeMapEntity.StartCoroutine(LookAtMoveCortinue());
        }

        private IEnumerator LookAtMoveCortinue()
        {
            int[] path = _NodeTypeMapEntity.PathFind(_StayNode, _DestNode);
            if (path == null)
            {
                yield break;
            }
            for (int j = 0; j < path.Length; j++)
            {
                Vector3 dirVector = _NodeTypeMapEntity.Nodes[path[j]] - _Transform.position;
                float normlizeAnge = Vector3.Angle(dirVector, _Transform.forward) / 180;
                Quaternion startQua = _Transform.rotation;
                Quaternion quaternion = Quaternion.LookRotation(dirVector, Vector3.up);
                for (float i = 0; i <= 1; i += Time.deltaTime / normlizeAnge)
                {
                    _Transform.rotation = Quaternion.Slerp(startQua, quaternion, i);
                    yield return null;
                }
                float dis = dirVector.magnitude;
                Vector3 dir = dirVector.normalized;
                for (float i = 0; i < dis; i += Speed)
                {
                    float num;
                    if (i > dis)
                    {
                        num = Speed - (i - dis);
                    }
                    else
                    {
                        num = Speed;
                    }
                    _Transform.Translate(num * dir, Space.World);
                    yield return null;
                }
                _StayNode = path[j];
            }
            yield break;
        }
    }
}
